package summit.tests
{
import flash.events.Event;

import summit.basecamp.helpers.TodoItemHelper;
import summit.basecamp.helpers.TodoListHelper;
import summit.data.AccountToken;
import summit.events.BasecampAPIEvent;
import summit.tests.tools.SummitTestContainer;
import summit.tests.tools.TestHelpers;

public class TodoItemHelperTests extends SummitTestContainer
{
	private var _todoListId:String;
	private var _todoItemId:String;
	
	public function TodoItemHelperTests(token:AccountToken)
	{
		super(token);
	}
	
	public function readTest(context:Object):void
	{
		super.initializeLoader(super.loader_OKComplete);
		loader.load(TodoItemHelper.read(_todoItemId));
	}
	
	public function listTest(context:Object):void
	{
		super.initializeLoader(super.loader_OKComplete);
		loader.load(TodoItemHelper.list(_todoListId));
	}
	
	public function completeUncompleteTest(context:Object):void
	{
		initializeLoader(loader_OKCheck);
		loader.load(TodoItemHelper.complete(_todoItemId));	
	}
	
	public function editUpdateTest(context:Object):void
	{
		initializeLoader(loader_OKEdit);
		loader.load(TodoItemHelper.edit(_todoItemId));
	}
	
	private function loader_OKEdit(e:BasecampAPIEvent):void
	{
		initializeLoader(super.loader_OKComplete);
		loader.load(TodoItemHelper.update(e.data));	
	}
	
	private function loader_OKCheck(e:BasecampAPIEvent):void
	{
		initializeLoader(super.loader_OKComplete);
		loader.load(TodoItemHelper.uncomplete(_todoItemId));
	}
	
	override protected function self_CompleteInit(e:Event):void
	{
		super.initializeLoader(loader_OKInitPrepareTodoList);
		loader.load(TodoListHelper.prepare(projectId));
	}
	
	private function loader_OKInitPrepareTodoList(e:BasecampAPIEvent):void
	{
		super.initializeLoader(loader_OKInitCreateTodoList);
		loader.load(TodoListHelper.create(e.data));
	}
	
	private function loader_OKInitCreateTodoList(e:BasecampAPIEvent):void
	{
		_todoListId = 
		TestHelpers.parseForIdFromResponseHeaders(e.responseHeaders);
		
		super.initializeLoader(loader_OKInitPrepareTodoItem);
		loader.load(TodoItemHelper.prepare(_todoListId));
	}
	
	private function loader_OKInitPrepareTodoItem(e:BasecampAPIEvent):void
	{
		super.initializeLoader(loader_OKInitCreateTodoItem);
		loader.load(TodoItemHelper.create(e.data));
	}
	
	private function loader_OKInitCreateTodoItem(e:BasecampAPIEvent):void
	{
		_todoItemId = 
		TestHelpers.parseForIdFromResponseHeaders(e.responseHeaders);
		super.self_CompleteInit(null);
	}
	
	public override function cleanUp():void
	{
		super.initializeLoader(loader_OKCleanUpTodoItemDestroy);
		loader.load(TodoItemHelper.destroy(_todoItemId));
	}
	
	private function loader_OKCleanUpTodoItemDestroy(e:BasecampAPIEvent):void
	{
		super.initializeLoader(loader_OKCleanUpTodoListDestroy);
		loader.load(TodoListHelper.destroy(_todoListId));
	}
	
	private function loader_OKCleanUpTodoListDestroy(e:BasecampAPIEvent):void
	{
		super.cleanUp();
	}
}
}